package com.tdr.myandroid.db;

import android.database.Cursor;

import com.tdr.myandroid.model.ActionObject;
import com.tdr.myandroid.model.EventObject;

import java.util.ArrayList;

/**
 * Created by Administrator on 2014/7/16.
 */
public class ActionsDao {
    private ArrayList<ActionObject> actionsList = new ArrayList<ActionObject>();

    public ArrayList<ActionObject> getActionsList() {
        return actionsList;
    }

    public void updateAllActions() {
        actionsList.clear();

        Cursor cur = DatabaseHelper.getDbHelper().getReadableDatabase().rawQuery("select * from actions", null);

        while (cur.moveToNext()) {
            int id = cur.getInt(cur.getColumnIndex("event_id"));
            int typeInt = cur.getInt(cur.getColumnIndex("action_type"));
            String targetString = cur.getString(cur.getColumnIndex("action_target"));
            String contentString = cur.getString(cur.getColumnIndex("action_content"));
            String extraString = cur.getString(cur.getColumnIndex("action_extra_info"));

            ActionObject newAction = EventObject.createAction(typeInt, id);
            newAction.setContentString(contentString);
            newAction.setExtraInfoString(extraString);
            newAction.setTargetString(targetString);
            actionsList.add(newAction);
        }
    }

    public ArrayList<ActionObject> getActionsById(int id ) {
        ArrayList<ActionObject> returnActionList = new ArrayList<ActionObject>();

        for (ActionObject singleAction : actionsList) {
            if (singleAction.getId() == id) {
                returnActionList.add(singleAction);
            }
        }
        return returnActionList;
    }
}
